#include "qipan.h"
#include<QPainter>
#include<QDebug>
#include<QPoint>
#include<QPair>
#include"mainwindow.h"
#include<QMessageBox>
qipan::qipan(QWidget *parent) : QWidget(parent),gameStarted(false)
{
    /*清除脏数据*/
    m_board=(Role **)malloc(sizeof(Role*)*m_boardSize);
    for(int idx=0;idx<m_boardSize;idx++)
    {
        m_board[idx]=(Role*)malloc(sizeof(Role)*m_boardSize);
    }
    for(int row=0;row<m_boardSize;row++)
    {
        for(int col=0;col<m_boardSize;col++)
        {
            m_board[row][col]=EMPTY;
        }
    }

    m_hoverFlag=false;

    /*捕获鼠标移动*/
    setMouseTracking(true);
    // 连接 MainWindow 的信号
    MainWindow* mainWindow = qobject_cast<MainWindow*>(parent);  // 获取 MainWindow 指针
    if (mainWindow) {
        connect(mainWindow, &MainWindow::gameStartedChanged, this, &qipan::onGameStartedChanged);
    }



}

void qipan::setBoardInfo(int row, int col, Role role)
{
     if(row>=0&&row<m_boardSize&&col>=0&&col<m_boardSize)

     {
         Move move = {row, col, role};
         moveHistory.push(move);  // 将该棋步推入历史记录栈
         m_board[row][col]=role;

     }

}

Role **qipan::getBoardInfo(int &boardSize)
{
    boardSize=m_boardSize;
    return reinterpret_cast<Role**>(m_board);

}

void qipan::resetBoard()
{
    // 清空棋盘
    for (int i = 0; i < m_boardSize; ++i) {
        for (int j = 0; j < m_boardSize; ++j) {
            m_board[i][j] = Role::EMPTY;  // 假设 Empty 是 Role 枚举的一个值，表示空位
        }
    }

    // 触发重绘
    update();
}



void qipan::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 启用抗锯齿效果
    // 绘制棋盘背景
    // 棋盘背景颜色
        QColor backgroundColor = QColor(211, 141, 206); // 淡灰色
        painter.fillRect(rect(), backgroundColor);
    /*绘制棋盘的网格线*/
    for(int idx=0;idx<m_boardSize;idx++)
    {
        /*绘制竖线*/
        painter.drawLine(m_margin+idx*m_cellSize,m_margin,m_margin+idx*m_cellSize,m_margin+(m_boardSize-1)*m_cellSize);
        /*绘制横线*/
        painter.drawLine(m_margin,m_margin+idx*m_cellSize,m_margin+(m_boardSize-1)*m_cellSize,m_margin+idx*m_cellSize);
    }
    /*绘制悬停的点*/
    if(m_hoverFlag&&(m_hoverRow>=0&&m_hoverRow<m_boardSize)&&(m_hoverCol>=0&&m_hoverCol<m_boardSize)&&(m_board[m_hoverRow][m_hoverCol]==EMPTY))
    {
        painter.setBrush(Qt::red);
        painter.drawEllipse(QPoint(m_margin+m_hoverRow*m_cellSize,m_margin+m_hoverCol*m_cellSize),m_cellSize/6,m_cellSize/6);
    }
    /*绘制棋子*/
    for(int i=0;i<m_boardSize;i++)
    {
        for(int j=0;j<m_boardSize;j++)
        {

            if((m_board[i][j]==PLATER))
            {
                painter.setBrush(Qt::black);
                painter.drawEllipse(QPoint(m_margin+i*m_cellSize,m_margin+j*m_cellSize),m_cellSize/3,m_cellSize/3);


            }
            else if(m_board[i][j]==COMPUTER)
            {
                painter.setBrush(Qt::white);
                painter.drawEllipse(QPoint(m_margin+i*m_cellSize,m_margin+j*m_cellSize),m_cellSize/3,m_cellSize/3);

            }
        }
    }


}

void qipan::mouseMoveEvent(QMouseEvent *event)
{

    int row=(event->x()-m_margin+m_cellSize/2)/m_cellSize;
    int col=(event->y()-m_margin+m_cellSize/2)/m_cellSize;
    /*判断棋盘是否在棋盘内部*/
    if(row>=0&&row<m_boardSize&&col>=0&&col<m_boardSize)
    {
        if(m_hoverRow!=row||m_hoverCol!=col)
        {
        m_hoverRow=row;
        m_hoverCol=col;
        m_hoverFlag=true;
        update();

        }
    }
    else
    {
        if(m_hoverFlag)
        {
            m_hoverFlag=false;
            /*移出棋盘小将坐标清空*/
            m_hoverRow=m_hoverCol=-1;
            /*更新画面*/
            update();
        }

    }



}

void qipan::mousePressEvent(QMouseEvent *event)
{
    if (!gameStarted) {
           QMessageBox::warning(this, "游戏未开始", "请先点击开始按钮开始游戏！");
           return;
       }
    else{

    int row=(event->x()-m_margin+m_cellSize/2)/m_cellSize;
    int col=(event->y()-m_margin+m_cellSize/2)/m_cellSize;
    /*判断棋盘是否在棋盘内部*/
    if(row>=0&&row<m_boardSize&&col>=0&&col<m_boardSize)
    {
        /*发信号*/
        emit chessClick(row,col);
//        m_board[row][col]=PLATER;
//        update();
    }
    }

}

void qipan::onGameStartedChanged(bool started)
{
    gameStarted=started;
}

void qipan::undoMove()
{

    if (moveHistory.empty()) {
           QMessageBox::information(this, "无法悔棋", "没有可以悔棋的步数！");
           return;
       }

       if (!gameStarted) {
           QMessageBox::information(this, "无法悔棋", "游戏未开始，无法进行悔棋！");
           return;
       }


    if (!moveHistory.empty()) {
        // 获取并移除最近的一步棋
        Move lastMove = moveHistory.top();
        moveHistory.pop();

        // 将该位置恢复为“空”状态
        m_board[lastMove.row][lastMove.col] = Role::EMPTY;

        // 触发重绘
        update();
    }

}

